package com.yang.mybatis.test;

import com.yang.mybatis.config.MybatisConfiguration;
import com.yang.mybatis.config.MybatisDataSource;
import com.yang.mybatis.config.MybatisEnvironment;
import com.yang.mybatis.config.MybatisSqlStatement;
import com.yang.mybatis.config.parser.IMybatisConfigurationParser;
import com.yang.mybatis.config.parser.IMybatisMapperParser;
import com.yang.mybatis.config.parser.XmlMybatisConfigurationParser;
import com.yang.mybatis.config.parser.XmlMybatisMapperParser;
import com.yang.mybatis.proxy.MapperProxy;
import com.yang.mybatis.proxy.MapperProxyFactory;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        IMybatisConfigurationParser iMybatisConfigurationParser = new XmlMybatisConfigurationParser();
        MybatisConfiguration mybatisConfiguration = iMybatisConfigurationParser.parser("mybatis-config.xml");

        IMybatisMapperParser iMybatisMapperParser = new XmlMybatisMapperParser();
        List<String> mapperPaths = mybatisConfiguration.getMapperPaths();
        for (String mapperPath : mapperPaths) {
            List<MybatisSqlStatement> mybatisSqlStatements = iMybatisMapperParser.parseMapper(mapperPath);
            Map<String, List<MybatisSqlStatement>> mapperNameGroupMap = mybatisSqlStatements.stream()
                    .collect(Collectors.groupingBy(MybatisSqlStatement::getNamespace));

            for (Map.Entry<String, List<MybatisSqlStatement>> entry : mapperNameGroupMap.entrySet()) {
                String mapperName = entry.getKey();
                List<MybatisSqlStatement> sqlSessionList = entry.getValue();
                mybatisConfiguration.putMybatisSqlStatementList(mapperName, sqlSessionList);
            }
        }

        MapperProxyFactory mapperProxyFactory = new MapperProxyFactory(mybatisConfiguration);
        IUserMapper userMapper = (IUserMapper) mapperProxyFactory.newInstance(IUserMapper.class);
        System.out.println(userMapper.queryUserName(1));
    }

//    public static void main(String[] args) {
//        IMybatisMapperParser mybatisMapperParser = new XmlMybatisMapperParser();
//        List<MybatisSqlStatement> mybatisSqlStatements = mybatisMapperParser.parseMapper("mapper/UserMapper.xml");
//        for (MybatisSqlStatement mybatisSqlStatement : mybatisSqlStatements) {
//            System.out.println("=================");
//            System.out.println("namespace:" + mybatisSqlStatement.getNamespace());
//            System.out.println("id:" + mybatisSqlStatement.getId());
//            System.out.println("sql:" + mybatisSqlStatement.getSql());
//        }
//    }

//    public static void main(String[] args) {
//        IMybatisConfigurationParser mybatisConfigurationParser = new XmlMybatisConfigurationParser();
//        MybatisConfiguration mybatisConfiguration = mybatisConfigurationParser.parser("mybatis-config.xml");
//        System.out.println("mapperPaths==========");
//        for (String mapperPath : mybatisConfiguration.getMapperPaths()) {
//            System.out.println(mapperPath);
//        }
//        System.out.println("environments========");
//        for (MybatisEnvironment environment : mybatisConfiguration.getEnvironments()) {
//            MybatisDataSource mybatisDataSource = environment.getMybatisDataSource();
//            System.out.println("id:" + environment.getId()
//                    + ",driver:" + mybatisDataSource.getDriver()
//                    + ",url:" + mybatisDataSource.getUrl()
//                    + ",username:" + mybatisDataSource.getUsername()
//                    + ",password:" + mybatisDataSource.getPassword());
//        }
//        System.out.println("defaultEnvironment=======");
//        MybatisEnvironment defaultMybatisEnvironment = mybatisConfiguration.getDefaultMybatisEnvironment();
//        MybatisDataSource mybatisDataSource = defaultMybatisEnvironment.getMybatisDataSource();
//        System.out.println("id:" + defaultMybatisEnvironment.getId()
//                + ",driver:" + mybatisDataSource.getDriver()
//                + ",url:" + mybatisDataSource.getUrl()
//                + ",username:" + mybatisDataSource.getUsername()
//                + ",password:" + mybatisDataSource.getPassword());
//    }
}
